package com.kongfuzi.student.ui.gaspay;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.kongfuzi.student.R;
import com.kongfuzi.student.bean.MessageEvent;
import com.kongfuzi.student.support.utils.Util;
import com.kongfuzi.student.ui.base.BasePopupWindow;

import de.greenrobot.event.EventBus;


/**
 * Created by cfan_yang on 2015/12/9.
 */
public class GasPayPopupWindow extends BasePopupWindow implements View.OnClickListener{

    private TextView mTv_wx;
    private TextView mTv_ali;
    private TextView mTv_cancel;
    private OnPayMethodListener mListener;
    /**
     * 初始化PopupWindow
     *
     * @param context
     * @param widthPercent  PopupWindow宽度占屏幕百分比
     * @param heightPercent PopupWindow高度占屏幕百分比
     */
    public GasPayPopupWindow(Activity context, float widthPercent, float heightPercent) {
        super(context, widthPercent, heightPercent);
    }

    @Override
    protected void initEvent() {
        mTv_wx.setOnClickListener(this);
        mTv_ali.setOnClickListener(this);
        mTv_cancel.setOnClickListener(this);
    }

    @Override
    protected void initView(Context context) {
        mTv_wx = (TextView) mConvertView.findViewById(R.id.tv_wx);
        mTv_ali = (TextView) mConvertView.findViewById(R.id.tv_ali);
        mTv_cancel = (TextView) mConvertView.findViewById(R.id.tv_cancel);
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.layout_popup_pay_method;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_wx:
                mTv_wx.setClickable(false);
                if(mListener!=null) {
                    mListener.onWxClicked();
                }
                break;
            case R.id.tv_ali:
                mTv_ali.setClickable(false);
                if(mListener!=null) {
                    mListener.onAliClicked();
                }
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        Util.lightOn((Activity) mContext);
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(((Activity) mContext).getWindow().getDecorView(), gravity, x, y);
        Util.lightOff((Activity) mContext);
        EventBus.getDefault().register(this);
    }

    public void onEvent(MessageEvent event) {
        if(MessageEvent.MessageType.BUTTON_PAY_ABLE.equals(event.messageType)) {
            mTv_ali.setClickable(true);
            mTv_wx.setClickable(true);
        }
    }

    public void setPayMethodListener(OnPayMethodListener listener) {
        this.mListener = listener;
    }

    public interface OnPayMethodListener {
        public void onAliClicked();
        public void onWxClicked();
    }
}
